package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Permission;
import com.itheima.service.PermissionService;
import com.itheima.service.impl.PermissionServiceImpl;
import com.itheima.utils.BeanUtil;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/permission/*")
public class PermissionServlet extends BaseServlet{
    private PermissionService permissionService=new PermissionServiceImpl();

    @Test
    //分页查询页面所有
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //当前页面pageNum
        String pageNum = req.getParameter("pageNum");
        //每页显示条数pageSize
        String pageSize = req.getParameter("pageSize");
        //查询方法
        List<Permission> list=permissionService.findAll(pageNum,pageSize);
        //req.setAttribute("permissionList",list);
        //查询结果分装到分页数据
        PageInfo<Permission> page=new PageInfo<Permission>(list);
        //数据发送到前台
        req.setAttribute("page",page);
        //路径跳转
        req.getRequestDispatcher("/pages/permission-list.jsp").forward(req,resp);
    }
    //新增保存
    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据封装对象
        Permission permission = BeanUtil.fillBean(req, Permission.class);
        //调用保存方法
        permissionService.save(permission);
        //保存成功,查询所有
        findAll(req,resp);
    }
    //删除
    public void deletePermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面id
        String id = req.getParameter("id");
        //调用删除方法
        permissionService.deleteById(id);
        //删除成功,查询所有
        findAll(req,resp);
    }





}
